/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpi;

import java.io.IOException;

/**
 *
 * @author users
 */
public class Mpi {

    public void controller(String[] args) {

        MPIController controller = new MPIController();
        controller.setPlatform(MPIPlatform.LINUX);
        ServerListCmd list = new ServerListCmd(controller);

        String commandFile = "command.ser";
        try {
            if (list.load(commandFile)) {
                System.out.println("Load Command List "+commandFile+":");
                //for (MPICmd cmd : list.Cmd) {
                   // System.out.println(list.Cmd.toString());
                //}
            } else {

            }
        }catch (Exception e){
                System.err.println("Error load file " + commandFile);
        }

        if (args[0].equals("-s") || args[0].equals("save")) {
            if (list.save(commandFile)) {
                System.out.println(commandFile + " Save Command List sucseful!");
            }
        } else if (args[0].equals("-l") || args[0].equals("load")) {
            System.out.println(commandFile + "\nCommand List:");
            System.out.println(list.Cmd.toString());
        } else if (args[0].equals("-e") || args[0].equals("exec")) {

            System.out.println("\nExecute command: " + args[1]);
            for (MPICmd cmd : list.Cmd) {
                if (cmd.getName().equals(args[1])) {
                    cmd.execCommand();
                }
            }
        } else if (args[0].equals("-a") || args[0].equals("add")) {

            System.out.println("\nAdd command: " + args[1]);
            MPICmd cmd = new MPICmd(args[1], controller);
            try{
                if (!args[2].equals("")){
                    cmd.setFileName(args[2]);
                }
            }catch(Exception e){
                
            }
            list.addCmd(cmd);

            if (list.save(commandFile)) {
                       System.out.println(commandFile + " Save Command List sucseful!");
            }
           
        } else if (args[0].equals("-d") || args[0].equals("delete")) {

            System.out.println("\nDelete command: " + args[1]);

            for (MPICmd cmd : list.Cmd) {

                if (cmd.getName().equals(args[1])) {
                    list.deleteCmd(cmd.getId());
                }
            }
        }


    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        Mpi mpi = new Mpi();
        mpi.controller(args);
    }
}
